package com.dingup.api.Util;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

/**
 * Created by wancheng on 2018/7/30.
 */
public class PropertyUtil {

    private static final String PROPERTY_PATH = "/application.yml";

    public static String getPropertyValue(String key) {
        Properties prop = new Properties();
        InputStream in = PropertyUtil.class.getResourceAsStream(PROPERTY_PATH);
        String value = null;
        try {
            prop.load(in);
            value = prop.getProperty(key).trim();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return value.trim();
    }

    public static String getPropertyValue(String key, String defaultValue) {
        String value = getPropertyValue(key);
        if (value == null) {
            value = defaultValue;
        }
        return value;
    }

    public static Integer getPropertyIntegerValue(String key, Integer defaultValue) {
        String value = getPropertyValue(key);
        if (value != null) {
            return Integer.valueOf(value);
        } else {
            return defaultValue;
        }
    }

    public static Boolean getPropertyBooleanValue(String key, Boolean defaultValue) {
        String value = getPropertyValue(key);
        if (value != null) {
            return Boolean.valueOf(value);
        } else {
            return defaultValue;
        }
    }
}
